$(function () {
    //跳转到本页之后读取cookie中是否有mid和token
    checkCookie();
    var mid = $.cookie('mid');
    var token = $.cookie('token');
    localStorage.removeItem("work_time");//localStorage清除预约时间
    localStorage.removeItem("doc_id");//localStorage清除预约医生id
    //tab日期
    var mydate = new Date();
    var year = mydate.getFullYear();
    var month = mydate.getMonth() + 1;
    var date = mydate.getDate();
    var week = mydate.getDay();
    var arr = ['周日', '周一', '周二', '周三', '周四', '周五', '周六', '周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    for (var i = 0; i < 7; i++) {
        $('.date_list li').eq(i).find('.week').html(arr[week + i]);
        $('.date_list li').eq(i).find('.month_date').html(month + '-' + (date + i));
        $('.date1 li').eq(i).find('.week').html(arr[week + i]);
        $('.date1 li').eq(i).find('.month_date').html(month + '-' + (date + i))
    }
    $('.date_list li').find('.week').eq(0).html('今天');
    $('.date1 li').find('.week').eq(0).html('今天');
    $('.date1 li').find('.month_date').eq(0).html(month + '-' + date);
    $('.date_active').on('click', function () {
        $(this).siblings().find('div').removeClass('tag');
        $(this).find('div').addClass('tag')
    });
    //指定医师的信息
    $.get(changeOrderURL + location.search, function (data) {
        var aid = data.aid;
        $(".load").css("display", "none");
        $(".list").html(template("doctor", data));
        //点击首页预约按钮
        //点击弹窗预约
        var id = data.doctor.data.id;//医生id
        $('.yuyue').on('click', function () {
            $('#all').show();
            $('body').css('position', 'fixed');
            var myScroll = new iScroll('wrapper', {
                vScroll: false,
                hScroll: true
            });
            //显示预约时间段
            $.get(yuyue + "?id=" + id + "&key=0", function (data) {
                $(".wait_tip").text("选择时间段");
                $(".choose").html(template("choose_date", data));
                $(".yy").on("click", function () {
                    var key = $('#all .tag').parent().index();
                    var time = $(this).attr('work_time');
                    localStorage.setItem("work_time", time);//localStorage预约时间
                    localStorage.setItem("doc_id", id);//localStorage预约医生id
                    var mtime = year + '-' + month + '-' + (parseInt(date) + parseInt(key)) + ' ' + time;
                    $.post(changeOrderURL, {aid: aid, mark_time: mtime}, function () {
                        window.location.href = 'my-appoint.html';
                    });
                });
            });
            $(".date_active").on("click", function () {
                $(".wait_tip").text("请稍等...");
                $(".choose").html("");
                $.get(yuyue + "?id=" + id + "&key=" + $(this).index(), function (data) {
                    $(".wait_tip").text("选择时间段");
                    $(".choose").html(template("choose_date", data));
                    $(".yy").on("click", function () {
                        var key = $('#all .tag').parent().index();
                        var time = $(this).attr('work_time');
                        localStorage.setItem("work_time", time);//localStorage预约时间
                        localStorage.setItem("doc_id", id);//localStorage预约医生id
                        var mtime = year + '-' + month + '-' + (parseInt(date) + parseInt(key)) + ' ' + time;
                        $.post(changeOrderURL, {aid: aid, mark_time: mtime}, function () {
                            window.location.href = 'my-appoint.html';
                        });
                    });
                });
            });
            //关闭弹框
            $('.close').on('click', function () {
                $('#all').hide();
                $('body').css('position', 'relative');
            });
        });
    });
});

